﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using URLWidgetDisplay.Classes;

namespace URLWidgetDisplay
{
    public partial class Appointment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var ClientId = Request.QueryString["patid"];
            var SortOrder = Request.QueryString["SortOrder"];
            var CurrentPage = "Appointment.aspx";
            var tempLink = new LinkButton();
            //var appointmentList = new IEnumerable<Appointment>();
            //if (!String.IsNullOrEmpty(ClientId))
            //    appointmentList = AppointmentRepository.GetAppointmentsByClientId(ClientId);
            //else
            var appointmentList = AppointmentRepository.GetAppointmentsByClientId(ClientId).OrderByDescending(a => a.AppointmentDate);
            switch (SortOrder)
            {
                case "date":
                    appointmentList = appointmentList.OrderBy(a => a.AppointmentDate);
                    uxAppointmentDate.PostBackUrl = CreateLink(CurrentPage, ClientId, "dateDesc");
                    break;
                case "dateDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.AppointmentDate);
                    uxAppointmentDate.PostBackUrl = CreateLink(CurrentPage, ClientId, "date");
                    break;
                case "startTime":
                    appointmentList = appointmentList.OrderBy(a => a.StartTime);
                    uxStartTime.PostBackUrl = CreateLink(CurrentPage, ClientId, "startTimeDesc");
                    break;
                case "startTimeDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.StartTime);
                    uxStartTime.PostBackUrl = CreateLink(CurrentPage, ClientId, "startTime");
                    break;
                case "endTime":
                    appointmentList = appointmentList.OrderBy(a => a.EndTime);
                    uxEndTime.PostBackUrl = CreateLink(CurrentPage, ClientId, "endTimeDesc");
                    break;
                case "endTimeDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.EndTime);
                    uxEndTime.PostBackUrl = CreateLink(CurrentPage, ClientId, "endTime");
                    break;
                case "staff":
                    appointmentList = appointmentList.OrderBy(a => a.StaffName);
                    uxStaff.PostBackUrl = CreateLink(CurrentPage, ClientId, "staffDesc");
                    break;
                case "staffDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.StaffName);
                    uxStaff.PostBackUrl = CreateLink(CurrentPage, ClientId, "staff");
                    break;
                case "program":
                    appointmentList = appointmentList.OrderBy(a => a.ProgramValue);
                    uxProgram.PostBackUrl = CreateLink(CurrentPage, ClientId, "programDesc");
                    break;
                case "programDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.ProgramValue);
                    uxProgram.PostBackUrl = CreateLink(CurrentPage, ClientId, "program");
                    break;
                case "scheduledSvc":
                    appointmentList = appointmentList.OrderBy(a => a.ScheduledServiceValue);
                    uxScheduledService.PostBackUrl = CreateLink(CurrentPage, ClientId, "scheduledSvcDesc");
                    break;
                case "scheduledSvcDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.ScheduledServiceValue);
                    uxScheduledService.PostBackUrl = CreateLink(CurrentPage, ClientId, "scheduledSvc");
                    break;
                case "status":
                    appointmentList = appointmentList.OrderBy(a => a.StatusValue);
                    uxStatus.PostBackUrl = CreateLink(CurrentPage, ClientId, "statusDesc");
                    break;
                case "statusDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.StatusValue);
                    uxStatus.PostBackUrl = CreateLink(CurrentPage, ClientId, "status");
                    break;
                case "posted":
                    appointmentList = appointmentList.OrderBy(a => a.PostedCode);
                    uxPosted.PostBackUrl = CreateLink(CurrentPage, ClientId, "postedDesc");
                    break;
                case "postedDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.PostedCode);
                    uxPosted.PostBackUrl = CreateLink(CurrentPage, ClientId, "posted");
                    break;
                case "missedVisit":
                    appointmentList = appointmentList.OrderBy(a => a.MissedVisitServiceValue);
                    uxMissedVisit.PostBackUrl = CreateLink(CurrentPage, ClientId, "missedVisitDesc");
                    break;
                case "missedVisitDesc":
                    appointmentList = appointmentList.OrderByDescending(a => a.MissedVisitServiceValue);
                    uxMissedVisit.PostBackUrl = CreateLink(CurrentPage, ClientId, "missedVisit");
                    break;
                default:
                    break;
            }
            uxRepeater.DataSource = appointmentList;
            uxRepeater.DataBind();
            var Crosstab = CreateCrosstab(appointmentList);
            uxRepeaterCrosstab.DataSource = Crosstab;
            uxRepeaterCrosstab.DataBind();
            if(!IsPostBack)
                InitiliazeSortLink(CurrentPage, ClientId);
        }

        private void InitiliazeSortLink(string Page, string ClientId)
        {
            uxAppointmentDate.PostBackUrl = CreateLink(Page, ClientId, "date");
            uxStartTime.PostBackUrl = CreateLink(Page, ClientId, "startTime");
            uxEndTime.PostBackUrl = CreateLink(Page, ClientId, "endTime");
            uxStaff.PostBackUrl = CreateLink(Page, ClientId, "staff");
            uxProgram.PostBackUrl = CreateLink(Page, ClientId, "program");
            uxScheduledService.PostBackUrl = CreateLink(Page, ClientId, "scheduledSvc");
            uxStatus.PostBackUrl = CreateLink(Page, ClientId, "status");
            uxPosted.PostBackUrl = CreateLink(Page, ClientId, "posted");
            uxMissedVisit.PostBackUrl = CreateLink(Page, ClientId, "missedVisit");
        }
        private string CreateLink(string Page, string ClientId, string SortOrder)
        {
            return Page + "?patid=" + ClientId + "&SortOrder=" + SortOrder;
        }
        private IEnumerable<Crosstab> CreateCrosstab(IEnumerable<Appointment> AppointmentList)
        {
            var Crosstab = new List<Crosstab>();
            var groups = AppointmentList.GroupBy(a => a.CrosstabCount);
            foreach (var row in groups)
            {
                Crosstab.Add(new Crosstab { Item = row.Key, NumberOfItems = AppointmentList.Count(a => a.CrosstabCount.Equals(row.Key)) });
            }
            Crosstab.Add(new Crosstab { Item = "Total # of services scheduled", NumberOfItems = AppointmentList.Count() });
            return Crosstab.OrderBy(i => i.Item);
        }
    }
}